﻿using LitJson;
using MobaCommon.Code;
using MobaCommon.Dto;
using MobaCommon.OpCode;
using MOBAServer.Cache;
using MOBAServer.Model;
using MOBAServer.Room;
using Photon.SocketServer;
using System;
using System.Collections.Generic;
using System.Linq;

namespace MOBAServer.Logic
{
    public class SelectHandler :SingeSend,IOpHandler
    {

        public Action<List<SelectModel>, List<SelectModel>> startFightEvent;
        private SelectCache selectCache = Caches.Select;

        /// <summary>
        /// 玩家缓存
        /// </summary>
        private PlayerCache playerCache = Caches.Player;
        /// <summary>
        /// 开始选人
        /// </summary>
        public void StartSelect(List<int> team1,List<int> team2)
        {
            //创建一个选人房间
            selectCache.CreateRoom(team1,team2);

        }

        public void OnDisconnect(MobaClient client)
        {
            selectCache.Offline(client,playerCache.GetId(client));
        }

        public void OnRequest(MobaClient client, byte subCode, OperationRequest request)
        {
            switch(subCode)
            {
                case OpSelect.Enter:
                    EnterSelect(client);
                    break;
                case OpSelect.Select:
                    OnSelect(client,(int)request[0]);
                    break;
                case OpSelect.Ready:
                    OnReady(client);
                    break;
                case OpSelect.Talk:
                    OnTalk(client,request[0].ToString());
                    break;
            }
        }
        /// <summary>
        /// 聊天
        /// </summary>
        /// <param name="client"></param>
        /// <param name="content"></param>
        private void OnTalk(MobaClient client, string content)
        {
            PlayerModel player = playerCache.GetModel(client);
            if (player == null)
                return;
            //给当前客户端所在的房间内所有人发一条消息:content
            SelectRoom room= selectCache.GetRoom(playerCache.GetId(client));
            
            string text = string.Format("{0}:{1}",player.Name,content);
            room.Broast(OpCode.SelectCode,OpSelect.Talk,0,"有玩家发言了",null, text);
        }

        /// <summary>
        /// 玩家准备确认选择
        /// </summary>
        /// <param name="client"></param>
        private void OnReady(MobaClient client)
        {
            int playerId = playerCache.GetId(client);
            SelectRoom room = selectCache.Ready(playerId);
            if(room==null)
            {
                Send(client,OpCode.SelectCode,OpSelect.Ready,-1,"确认失败");
                return;
            }
            //告诉房间所有人 此人确认选择了
            room.Broast(OpCode.SelectCode, OpSelect.Ready, 0, "有人确认选择了", null, playerId);

            //判断是否全部准备了
            if(room.IsAllReady)
            {
                startFightEvent(room.team1Dic.Values.ToList(),room.team2Dic.Values.ToList());
                //给客户端发送消息
                room.Broast(OpCode.SelectCode,OpSelect.StartFight,0,"准备进入战斗场景",null);
                //room.Broast(OpCode.SelectCode, OpSelect.Ready, 0, "有人确认选择了", null, playerId);
                selectCache.Destroy(room.id);
            }
        }
        /// <summary>
        /// 玩家选择英雄
        /// </summary>
        /// <param name="client"></param>
        /// <param name="heroId"></param>
        private void OnSelect(MobaClient client,int heroId)
        {
            int playerId = playerCache.GetId(client);
            //给房间内的所有人发一条消息：谁选了什么英雄(playerId,heroId)
            SelectRoom room =   selectCache.Select(playerId, heroId);
            MobaApplication.LogInfo(room+"!@!@#$!@#$!@#$!@#$!@#$!@#$!@#$!@#$!@#$!@#$!@$!@#$");
            if (room==null)
            {
                Send(client,OpCode.SelectCode,OpSelect.Select,-1,"选择失败");
                return;
            }
            else
            {
                room.Broast(OpCode.SelectCode,OpSelect.Select,0,"有人选择英雄了",null,playerId,heroId);
            }
        }
        private void EnterSelect(MobaClient client)
        {
            SelectRoom room= selectCache.Enter(playerCache.GetId(client),client) ;
            if (room == null)
                return;
            //先给此客户端发一个房间模型

            Send(client,OpCode.SelectCode,OpSelect.GetInfo,0,"获取房间模型",JsonMapper.ToJson(room.team1Dic.Values.ToArray()),JsonMapper.ToJson(room.team2Dic.Values.ToArray()));
            //再给房间内的客户端发一条消息 有人进入了
            room.Broast(OpCode.SelectCode,OpSelect.Enter,0,"有玩家进入",client,playerCache.GetId(client));
        }

    }
}
